public class StringBufferDemo {
    private String str = "";

    public StringBufferDemo() {
    }

    public StringBufferDemo(String str) {
        this.str = str;
    }

    // 接收字符串
    public void append(String str) {
        this.str += str;
    }

    // 接收字符
    public void append(char str) {
        String newString = new String(String.valueOf(str));
        this.str += newString;
    }

    // 将之前接收到的字符拼接到一起返回
    public String toString() {
        return this.str;
    }

    // 清除之前的输入内容
    public void clear() {
        this.str = "";
    }

    // 将之前输入内容反转
    public String reverse() {
        char[] newChar = this.str.toCharArray();
        for (int i = 0; i < str.length() / 2; i++) {
            char temp = newChar[i];
            newChar[i] = newChar[str.length() - i - 1];
            newChar[str.length() - i - 1] = temp;
        }
        return new String(newChar);
    }

    // 将指定位置的字符串反转
    public String reverse(int from, int to) {
        char[] newChar = this.str.toCharArray();
        for (int i = from; i <= (to - from)/2; i++) {
            char temp = newChar[i];
            newChar[i] = newChar[to - i];
            newChar[to - i] = temp;
        }
        return new String(newChar);
    }

    public static void main(String[] args) {
        StringBufferDemo demo = new StringBufferDemo();
        demo.append("abc");
        demo.append("def");
        demo.append("g");
        demo.append("h");
        System.out.println(demo.toString());
        System.out.println(demo.reverse());
        System.out.println(demo.reverse(1,7));
        demo.clear();
        System.out.println(demo);
    }

}
